package com.app.view.manager 
{
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Rectangle;

	import com.peachcastle.debug.Printer;
	
	public class LayerManagerMediator extends Mediator implements IMediator
	{
		public static const NAME:String 			= "LayerManagerMediator";
		public static const SET_ROOT_LAYER:String 	= NAME + "_SetRootLayer";
		public static const SET_DIALOG_LAYER:String = NAME + "_SetDialogLayer";
		public static const ADD_IN_LAYER:String 	= NAME + "_AddInLayer";
		
		public static const GET_LAYERS:String 		= NAME + "_GetLayers";
		
		////////////////////////////////////////////////////////////////////////////////
		// 图层名称
		public static const HALL_LAYER	:int = 0;		// 大厅层
		public static const ROOM_LAYER	:int = 1;		// 房间层
		public static const DIALOG_LAYER:int = 2;		// 对话框层
		public static const TIPS_LAYER	:int = 3;		// 游戏提示层
		public static const FEED_BACK_LAYER:int = 4;	// 反馈
		public static const PRINT_LAYER	:int = 5;		//日志打印层		
		public static const BITMAP_LAYER:int = 6;		//屏蔽层;
		// 图层数量
		private const LAYER_COUNT:int = 7;				// 图层数量
		////////////////////////////////////////////////////////////////////////////////
		//
		private var _rootLayer:DisplayObjectContainer = null;
		private var _manager:Array = null;
		
		/**
		 * 构造
		 */
		public function LayerManagerMediator() 
		{
			super(NAME);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				LayerManagerMediator.SET_ROOT_LAYER,
				LayerManagerMediator.SET_DIALOG_LAYER,
				LayerManagerMediator.ADD_IN_LAYER
			];
		}	
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var param:* = notification.getBody();
			switch ( name )
			{
				case LayerManagerMediator.SET_ROOT_LAYER:
					setRootLayer(param[0], param[1], param[2]);
					
					Printer.initScreenWrite(this);
					Printer.canPrint = true;
			
					break;
					
				case LayerManagerMediator.SET_DIALOG_LAYER:
					(param as Array).push(getLayer(LayerManagerMediator.DIALOG_LAYER));
					sendNotification(DialogManagerMediator.SET_DIALOG_LAYER, param);
			
					break;
					
				case LayerManagerMediator.ADD_IN_LAYER:
					getLayer(param[0]).addChild(param[1]);
					
					sendNotification(LayerManagerMediator.GET_LAYERS, [_manager]);
			
					break;
			}
		}				

		/**
		 * 设置舞台容器
		 * @param	rootLayer
		 */
		public function setRootLayer(rootLayer:DisplayObjectContainer, stageWidth:int, stageHeight:int):void
		{
			_rootLayer = rootLayer;
			initLayers(stageWidth, stageHeight);
		}
		
		/**
		 * 初始化图层
		 */
		private function initLayers(stageWidth:int, stageHeight:int):void
		{
			var rect:Rectangle = new Rectangle(0, 0, stageWidth, stageHeight);		

			_manager = [];
			var layer:Sprite = null;
			for (var i:int = 0; i < LAYER_COUNT; i++)
			{
				layer = new Sprite();
				layer.scrollRect = rect;
				layer.tabChildren = false;
				_rootLayer.addChild(layer);
				_manager[i] = layer;
				layer = null;
			}
			
			// 打印层不感应鼠标事件
			_manager[PRINT_LAYER].mouseEnabled = false;
			
		}
		
		/**
		 * 获取图层
		 * @param	layerType
		 * @return
		 */
		public function getLayer(layerType:int):Sprite		
		{
			return _manager[layerType];
		}
		
		/**
		 * 添加到图层
		 * @param	target		目标对象
		 * @param	layerType	图层类型
		 * @param	depthIndex	Z轴深度
		 */
		public function addToLayer(target:DisplayObject, layerType:int, depthIndex:int = -1):void			
		{			
			var layer:Sprite = _manager[layerType];
			if (depthIndex < 0 || depthIndex > layer.numChildren - 1)			
			{
				layer.addChild(target);
				return;
			}
			
			//
			layer.addChildAt(target, depthIndex);
		}	
		
		/**
		 * 把对象从指定层移除
		 * @param	target
		 */
		public function removeFromLayer(target:DisplayObject):void		
		{
			if (target == null) return;
			target.parent && target.parent.removeChild(target);
		}
		
		/**
		 * 显示列表根对象
		 */
		public function get rootLayer():DisplayObjectContainer { return _rootLayer; }
	
	}

}